package com.hhsjtest.composiable.logtest

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.widget.FrameLayout
import android.view.ViewGroup

class NormalFrameLayout : FrameLayout {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        val action = ev.action
        if (action == MotionEvent.ACTION_MOVE) {
            Log.e("HHSJ", "    ++++父视图 intercept 进入 move 判断")
            val viewGroupClass = ViewGroup::class.java
            try {
                val field = viewGroupClass.getDeclaredField("mFirstTouchTarget")
                field.isAccessible = true
                val firstTouchTarget = field[this]
                if (firstTouchTarget == null) {
                    Log.e("HHSJ", "    ++++mFirstTouchTarget = null")
                } else {
                    Log.e("HHSJ", "    ++++mFirstTouchTarget = " + firstTouchTarget.hashCode())
                    val field1 = firstTouchTarget.javaClass.getField("child")
                    field1.isAccessible = true
                    val child = field1[firstTouchTarget]
                    if (child != null) {
                        Log.e("HHSJ", "    ++++TouchTarget child " + child.hashCode())
                        Log.e("HHSJ", "    ++++FrameLayout child " + getChildAt(0).hashCode())
                    } else {
                        Log.e("HHSJ", "    ++++TouchTarget child = null")
                    }
                }
            } catch (e: NoSuchFieldException) {
                e.printStackTrace()
            } catch (e: IllegalAccessException) {
                e.printStackTrace()
            }
        }
        return super.onInterceptTouchEvent(ev)
    }

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        val action = ev.action
        if (action == MotionEvent.ACTION_DOWN) {
            Log.e("HHSJ", "    ++++父视图 dispatch down")
        }
        if (action == MotionEvent.ACTION_MOVE) {
            Log.e("HHSJ", "    ++++父视图 dispatch move")
        }
        if (action == MotionEvent.ACTION_CANCEL) {
            Log.e("HHSJ", "    ++++父视图 dispatch cancel===")
        }
        if (action == MotionEvent.ACTION_UP) {
            Log.e("HHSJ", "    ++++父视图 dispatch up===")
        }
        return super.dispatchTouchEvent(ev)
    }

    override fun onTouchEvent(ev: MotionEvent): Boolean {
        val action = ev.action
        if (action == MotionEvent.ACTION_MOVE) {
            Log.e("HHSJ", "    ++++父视图 touch 进入 move 判断")
            Log.e("HHSJ", "    ++++父视图 拦截后处理 move")
            val viewGroupClass = ViewGroup::class.java
            try {
                val field = viewGroupClass.getDeclaredField("mFirstTouchTarget")
                field.isAccessible = true
                val firstTouchTarget = field[this]
                if (firstTouchTarget == null) {
                    Log.e("HHSJ", "    ++++touch mFirstTouchTarget = null")
                } else {
                    Log.e(
                        "HHSJ",
                        "    ++++touch mFirstTouchTarget = " + firstTouchTarget.hashCode()
                    )
                    val field1 = firstTouchTarget.javaClass.getField("child")
                    field1.isAccessible = true
                    val child = field1[firstTouchTarget]
                    if (child != null) {
                        Log.e("HHSJ", "    ++++TouchTarget child " + child.hashCode())
                        Log.e("HHSJ", "    ++++FrameLayout child " + getChildAt(0).hashCode())
                    } else {
                        Log.e("HHSJ", "    ++++TouchTarget child = null")
                    }
                }
            } catch (e: NoSuchFieldException) {
                e.printStackTrace()
            } catch (e: IllegalAccessException) {
                e.printStackTrace()
            }
        }
        if (action == MotionEvent.ACTION_DOWN) {
            Log.e("HHSJ", "    ++++父视图 touch down")
        }
        if (action == MotionEvent.ACTION_CANCEL) {
            Log.e("HHSJ", "    ++++父视图 touch cancel===")
        }
        if (action == MotionEvent.ACTION_UP) {
            Log.e("HHSJ", "    ++++父视图 touch up===")
        }
        return super.onTouchEvent(ev)
    }
}